﻿using System;
using System.IO;
using System.Text;
using Vive.Crypto;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "SCTO加密存储演示";
            Console.Write("请输入测试文件路径:");
            var filePath = Console.ReadLine();
            if (!File.Exists(filePath))
            {
                Console.WriteLine("文件不存在");
                return;
            }
            var content = File.ReadAllText(filePath, Encoding.UTF8);
            Console.WriteLine("以下为原文件内容：");
            Console.WriteLine(content);
            Console.WriteLine();
            var sm3 = CryptoFactory.CreateHashing(HashingProviderType.SM3);
            sm3.Encoding = Encoding.UTF8;
            var sm3Res = sm3.Signature(content, null);
            Console.WriteLine($"SM3签名结果：{sm3Res}");
            Console.Write("输入SM4加密密钥(留空则使用SM3签名结果作为密钥)：");
            var userKey = Console.ReadLine();
            var sm4KeyText = string.IsNullOrEmpty(userKey) ? sm3Res : userKey;
            var sm4 = CryptoFactory.CreateSymmetric(SymmetricProviderType.SM4);
            var sm4Key = sm3.Signature(sm4KeyText, null);
            var sm4Res = sm4.Encrypt(content, sm4Key);
            Console.WriteLine("以下为SM4加密结果：");
            Console.WriteLine(sm4Res);
            Console.WriteLine("已将加密结果储存为sm4.txt");
            File.WriteAllText("sm4.txt", sm4Res);
            Console.Write("输入密钥解密：");
            var userDecryptKey = Console.ReadLine();
            var sm4Decrypted = sm4.Decrypt(sm4Res, string.IsNullOrEmpty(userKey) ? sm3Res : sm3.Signature(userDecryptKey, null));
            Console.WriteLine("以下为解密结果：");
            Console.WriteLine(sm4Decrypted);
        }
    }
}
